package main

import (
	"log"
	"os"
	"path"
)

//var data = [][]string{{"Line1", "Hello Readers of"}, {"Line2", "golangcode.com"}}
//
//func main() {
//	file, err := os.Create("result.csv")
//	checkError("Cannot create file", err)
//	defer file.Close()
//
//	writer := csv.NewWriter(file)
//	defer writer.Flush()
//
//	for _, value := range data {
//		err := writer.Write(value)
//		checkError("Cannot write to file", err)
//	}
//}
//
func checkError(message string, err error) {
	if err != nil {
		log.Println(message, err)
	}
}

func createFile(absolutePath string) (*os.File, error) {
	errDir := os.MkdirAll(path.Dir(absolutePath), 0777)
	if errDir != nil {
		return nil, errDir
	} else {
		if file, errFile := os.Create(absolutePath); errFile != nil {
			return file, nil
		} else {
			return nil, errFile
		}
	}
}

/*
func main() {

	titles := []string{"Location ID", "消息", "创建时间", "修改时间"}
	data := []string{"1", "消息1", "2021-11-25 11:06:23", "2021-11-25 11:06:23"}

	t := time.Now()
	dateFormat := "200601"
	dataDate := t.Format(dateFormat)

	fileName := fmt.Sprintf("%s-%s.csv", "CME终端数据", dataDate)
	dataPath := "cmeMarketData"
	uploadDir := "f:/csvTest/cmeMarketData/" + dataPath + "/" + dataDate + "/" + fileName
	fmt.Println("uploadDir:", uploadDir)
	//finalName := path.Join(uploadDir, dataPath, dataDate, fileName)
	file, err := createFile(uploadDir)
	checkError("Cannot create file", err)
	defer file.Close()

	file.WriteString("\xEF\xBB\xBF") // 写入UTF-8 BOM

	writer := csv.NewWriter(file)
	//defer writer.Flush()

	//写入标题
	err = writer.Write(titles)
	writer.Flush()
	checkError("Cannot write file title:", err)

	//写入文件内容
	err = writer.Write(data)
	writer.Flush()
	checkError("Cannot write file data:", err)
}
*/
